package com.panshi.controller.question;


import com.panshi.api.QuestionApi;
import com.panshi.api.SortApi;
import com.panshi.bean.PageBean;
import com.panshi.po.QuestionPO;
import com.panshi.po.SortPO;
import com.panshi.vo.PageModel;
import com.panshi.vo.QFindVagueVo;
import com.panshi.vo.QuestionUpdateVO;
import com.panshi.vo.ResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@Slf4j
public class QuestionController {

    //注入问题接口
    @Autowired
    QuestionApi questionApi;

    //注入分类接口
    @Autowired
    SortApi sortApi;


    @GetMapping("question-home")
    public String queryHome(){
        return "question/question-management";
    }

    @PostMapping("queryAll")
    @ResponseBody
    public PageModel<QuestionPO> queryAll(PageModel<QuestionPO> pageModel){
        PageBean pageVO = new PageBean();
        pageVO.setCurrentPageSize(pageModel.getPageSize());
        pageVO.setCurrentPage(pageModel.getCurrentPage());
        List<QuestionPO> list=questionApi.findQuestionByAll(pageVO).getData();
        Integer integer=questionApi.findCount();
        pageModel.setList(list);
        pageModel.setCount(integer);
        return pageModel;
    }

    @PostMapping("limitQuery/{vague}")
    @ResponseBody
    public PageModel<QuestionPO> limitQuery(@PathVariable String vague, PageModel<QuestionPO> pageModel){
        log.info("进来了=================={}",vague);
        PageBean pageVO = new PageBean();
        pageVO.setCurrentPageSize(pageModel.getPageSize());
        pageVO.setCurrentPage(pageModel.getCurrentPage());


        QFindVagueVo qFindVagueVo = new QFindVagueVo();
        qFindVagueVo.setVague(vague);
        qFindVagueVo.setCurrentPage(pageVO.getCurrentPage());
        qFindVagueVo.setCurrentPageSize(pageVO.getCurrentPageSize());

        List<QuestionPO> list =questionApi.findQuestionByVague(qFindVagueVo).getData();
        Integer integer=questionApi.findByVagueCount(vague);
        pageModel.setList(list);
        pageModel.setCount(integer);
        return pageModel;

    }

    //根据问题id出现问题信息以及获取分类信息
    @GetMapping("update/{id}")
    @ResponseBody
    public ModelAndView limitQuery(@PathVariable Integer id){
        ModelAndView mv = new ModelAndView("question/question-edit");
        //问题信息
        QuestionPO questionPO=questionApi.findQuestionById(id).getData();
        //获取所有分类
        List<SortPO> list=sortApi.totalQuery().getData();
        mv.addObject("SortPO",list);
        mv.addObject("questionPO",questionPO);
        return mv;
    }

    @PostMapping("updateQuestion")
    @ResponseBody
    public ResponseVO updateQuestion(QuestionUpdateVO questionUpdateVO){
        if(questionUpdateVO.getState() == 1){
            questionUpdateVO.setSolddate(new Date());
        }
        questionUpdateVO.setUpdatedate(new Date());
        log.info("修改对象=============={}",questionUpdateVO);
        Integer integer=questionApi.questionUpdate(questionUpdateVO).getData();
        if(integer == 1){
            return new ResponseVO("200","修改成功",true);
        }
        return new ResponseVO("500","修改失败",false);
    }

    @PostMapping("deleteQuestion")
    @ResponseBody
    public ResponseVO deleteQuestion(QuestionUpdateVO questionUpdateVO){
        if(questionUpdateVO.getState() == 1){
            questionUpdateVO.setSolddate(new Date());
        }
        questionUpdateVO.setUpdatedate(new Date());
        log.info("questionUpdateVO=={}",questionUpdateVO);
        Integer integer=questionApi.questionUpdate(questionUpdateVO).getData();
        if(integer == 1){
            return new ResponseVO("200","删除成功",true);
        }
        return new ResponseVO("500","删除失败",false);
    }


}
